<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input v-model="queryParams.title" placeholder="请输入标题" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="内容类型" prop="contentType">
        <el-select v-model="queryParams.contentType" placeholder="请选择内容类型" clearable>
          <el-option label="新闻动态" value="1" />
          <el-option label="行业新闻" value="2" />
          <el-option label="公司简介" value="3" />
          <el-option label="最新活动" value="4" />
          <el-option label="样板间展示" value="5" />
          <el-option label="房型效果图展示" value="6" />
          <el-option label="工艺间展示" value="7" />
          <el-option label="主要品牌展示" value="8" />
          <el-option label="辅助品牌展示" value="9" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['quan:content:add']">
          新增
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contentList" @selection-change="handleSelectionChange" border>
      <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true" />
      <el-table-column label="内容类型" align="center" prop="contentType">
        <template #default="scope">
          <span v-if="scope.row.contentType === '1'">新闻动态</span>
          <span v-else-if="scope.row.contentType === '2'">行业新闻</span>
          <span v-else-if="scope.row.contentType === '3'">公司简介</span>
          <span v-else-if="scope.row.contentType === '4'">最新活动</span>
          <span v-else-if="scope.row.contentType === '5'">样板间展示</span>
          <span v-else-if="scope.row.contentType === '6'">房型效果图展示</span>
          <span v-else-if="scope.row.contentType === '7'">工艺间展示</span>
          <span v-else-if="scope.row.contentType === '8'">主要品牌展示</span>
          <span v-else-if="scope.row.contentType === '9'">辅助品牌展示</span>
          <span v-else>{{ scope.row.contentType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button type="text" icon="View" @click="handlePreview(scope.row)" v-hasPermi="['quan:content:query']">
            预览
          </el-button>
          <el-button type="text" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['quan:content:edit']">
            修改
          </el-button>
          <el-button type="text" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['quan:content:remove']">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改内容生产对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" append-to-body>
      <el-form ref="contentRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="内容类型" prop="contentType">
          <el-select v-model="form.contentType" placeholder="请选择内容类型">
            <el-option label="新闻动态" value="1" />
            <el-option label="行业新闻" value="2" />
            <el-option label="公司简介" value="3" />
            <el-option label="最新活动" value="4" />
            <el-option label="样板间展示" value="5" />
            <el-option label="房型效果图展示" value="6" />
            <el-option label="工艺间展示" value="7" />
            <el-option label="主要品牌展示" value="8" />
            <el-option label="辅助品牌展示" value="9" />
          </el-select>
        </el-form-item>
        <el-form-item label="内容" prop="content">
          <editor v-model="form.content" :min-height="192" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 内容预览对话框 -->
    <el-dialog title="内容预览" v-model="previewOpen" width="800px" append-to-body>
      <div class="preview-content">
        <h2 style="text-align: center">{{ previewForm.title }}</h2>
        <div v-html="previewForm.content"></div>
      </div>
    </el-dialog>
  </div>
</template>

<script setup name="Content">
import { listContent, getContent, delContent, addContent, updateContent, exportContent } from '@/api/quan/content'
import Editor from '@/components/Editor/index.vue'

const { proxy } = getCurrentInstance()

const contentList = ref([])
const open = ref(false)
const previewOpen = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref('')
const dateRange = ref([])

const contentTypeOptions = [
  { label: '新闻动态', value: '1' },
  { label: '行业新闻', value: '2' },
  { label: '公司简介', value: '3' },
  { label: '最新活动', value: '4' },
  { label: '样板间展示', value: '5' },
  { label: '房型效果图展示', value: '6' },
  { label: '工艺间展示', value: '7' },
  { label: '主要品牌展示', value: '8' },
  { label: '辅助品牌展示', value: '9' }
]

const data = reactive({
  form: {},
  previewForm: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    title: null,
    content: null,
    contentType: null
  },
  rules: {
    title: [{ required: true, message: '标题不能为空', trigger: 'blur' }],
    contentType: [{ required: true, message: '内容类型不能为空', trigger: 'change' }],
    content: [{ required: true, message: '内容不能为空', trigger: 'blur' }]
  }
})

const { queryParams, form, previewForm, rules } = toRefs(data)

/** 查询内容生产列表 */
function getList() {
  loading.value = true
  listContent(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
    contentList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 表单重置 */
function reset() {
  form.value = {
    componentId: null,
    title: null,
    content: '<p></p>',
    contentType: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null,
    remark: null
  }
  proxy.resetForm('contentRef')
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm('queryRef')
  handleQuery()
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.componentId)
  single.value = selection.length !== 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  form.value.content = '<p></p>'
  open.value = true
  title.value = '添加内容'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const componentId = row.componentId || ids.value[0]
  getContent(componentId).then(response => {
    form.value = response.data
    open.value = true
    title.value = '修改内容'
  })
}

/** 预览按钮操作 */
function handlePreview(row) {
  getContent(row.componentId).then(response => {
    previewForm.value = response.data
    previewOpen.value = true
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['contentRef'].validate(valid => {
    if (valid) {
      if (form.value.componentId != null) {
        updateContent(form.value).then(response => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          getList()
        })
      } else {
        addContent(form.value).then(response => {
          proxy.$modal.msgSuccess('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const componentIds = row.componentId || ids.value
  proxy.$modal
    .confirm('是否确认删除')
    .then(function () {
      return delContent(componentIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    'quan/content/export',
    {
      ...queryParams.value
    },
    `content_${new Date().getTime()}.xlsx`
  )
}

onMounted(() => {
  getList()
})
</script>

<style scoped>
.preview-content {
  padding: 20px;
  line-height: 1.6;
  overflow-y: auto;
  max-height: 60vh;
}

.content-html {
  overflow-x: hidden;
  word-break: break-word;
}

.content-html :deep(img) {
  max-width: 100%;
  height: auto;
}
</style>
